﻿using MiniFox.Infrastructure;
using Newtonsoft.Json;
using MiniFox.Net;
using MiniFox.Net.Http;
using MiniFox.Utility;

namespace Dispatcher.Tasks.Models
{
    public class QueueItem
    {
        string _queueName;
        string _executionId;
        HttpService _service;
        protected internal QueueItem(string queueName, string executionId, HttpService service)
        {
            this._queueName = queueName;
            this._executionId = executionId;
            this._service = service;
        }

        public string QueueName
        {
            get { return _queueName; }
        }

        public string ExecutionID
        {
            get { return _executionId; }
        }

        public MiniFox.Platform.ApplicationIdentity Identity { get; set; }

        [JsonIgnore]
        public string AccessToken { get; set; }

        [JsonConverter(typeof(DateTimeJsonConverter))]
        public DateTimeOffset CreationTime { get; set; }

        public object OrignalData { get; set; }

        public int RetryCount { get; set; }

        public object Query { get; set; }

        public object Headers { get; set; }

        [JsonIgnore]
        public HttpService Service
        {
            get
            {
                return this._service;
            }
        }

        public string Url
        {
            get
            {
                if (this.Service == null)
                    return null;
                return this.Service.FullURI.ToString();
            }
        }

        protected virtual object Request
        {
            get
            {
                return OrignalData;
            }
        }

        protected internal async virtual Task<Response> ExecuteAsync()
        {
            Response response = await Service.PostAsync<object, Response>(Request, Query, Headers);
            return response;
        }

    }
}
